home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / ansi / stdio / filbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-23  |  1.3 KB  |  61 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <go32.h>
  8. #include <libc/file.h>
  9. #include <libc/stdiohk.h>
  10.  
  11. int
  12. _filbuf(FILE *f)
  13. {
  14.   int size;
  15.   char c;
  16.  
  17.   if (f->_flag & _IORW)
  18.     f->_flag |= _IOREAD;
  19.  
  20.   if ((f->_flag&_IOREAD) == 0)
  21.     return EOF;
  22.   if (f->_flag&(_IOSTRG|_IOEOF))
  23.     return EOF;
  24.  tryagain:
  25.   if (f->_base==NULL) {
  26.     if (f->_flag&_IONBF) {
  27.       f->_base = &c;
  28.       goto tryagain;
  29.     }
  30.     size = _go32_info_block.size_of_transfer_buffer;
  31.     if ((f->_base = malloc(size)) == NULL) {
  32.       f->_flag |= _IONBF;
  33.       goto tryagain;
  34.     }
  35.     f->_flag |= _IOMYBUF;
  36.     f->_bufsiz = size;
  37.   }
  38.   if (f == stdin) {
  39.     if (stdout->_flag&_IOLBF)
  40.       fflush(stdout);
  41.     if (stderr->_flag&_IOLBF)
  42.       fflush(stderr);
  43.   }
  44.   f->_cnt = read(fileno(f), f->_base,
  45.            f->_flag & _IONBF ? 1 : f->_bufsiz);
  46.   f->_ptr = f->_base;
  47.   if (f->_flag & _IONBF && f->_base == &c)
  48.     f->_base = NULL;
  49.   if (--f->_cnt < 0) {
  50.     if (f->_cnt == -1) {
  51.       f->_flag |= _IOEOF;
  52.       if (f->_flag & _IORW)
  53.     f->_flag &= ~_IOREAD;
  54.     } else
  55.       f->_flag |= _IOERR;
  56.     f->_cnt = 0;
  57.     return EOF;
  58.   }
  59.   return *f->_ptr++ & 0377;
  60. }
  61.